{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "# Convergence of Newton's Method"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 1,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import numpy as np\n",
        "import matplotlib.pyplot as pt"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 2,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "def f(x):\n",
        "    return np.exp(x) - 2"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 3,
      "metadata": {
        "collapsed": false
      },
      "outputs": [
        {
          "data": {
            "text/plain": [
              "[<matplotlib.lines.Line2D at 0x7fca94ed4048>]"
            ]
          },
          "execution_count": 3,
          "metadata": {},
          "output_type": "execute_result"
        },
        {
          "data": {
            "image/png": "iVBORw0KGgoAAAANSUhEUgAAAYAAAAD8CAYAAAB+UHOxAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDMuMC4yLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvOIA7rQAAIABJREFUeJzt3Xl8VfWd//HXJxsJWdgJOwFFUBGQIEodK7G14jIubW21rVqrxc6MXWamM7X19+t0+XU6bX/tjDO2Vaf6U+sSu7kUrYg20VpxIcq+CciSBMIOuSFkufn8/siFiWkCN8nNPXd5Px+P+8g5537P/X4+RM8n93vO+R5zd0REJP1kBB2AiIgEQwVARCRNqQCIiKQpFQARkTSlAiAikqZUAERE0pQKgIhImlIBEBFJUyoAIiJpKivoAE5k+PDhXlJS0qt9GxoayM/Pj21ACU45p750yxeUc09VVVXtdfcR0bRN6AJQUlLCsmXLerVvZWUl8+fPj21ACU45p750yxeUc0+Z2bZo22oISEQkTakAiIikKRUAEZE0pQIgIpKmVABERNKUCoCISJpSARARSVMqACIiCeSP6+tYvLWF1nBbv/elAiAikkB+uXQbL25rITPD+r0vFQARkQQRamrlz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 with 1 Axes>"
            ]
          },
          "metadata": {
            "needs_background": "light"
          },
          "output_type": "display_data"
        }
      ],
      "source": [
        "xgrid = np.linspace(-2, 3, 1000)\n",
        "pt.grid()\n",
        "pt.plot(xgrid, f(xgrid))"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "What's the true solution of $f(x)=0$?"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 4,
      "metadata": {
        "collapsed": false
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "0.6931471805599453\n",
            "0.0\n"
          ]
        }
      ],
      "source": [
        "xtrue = np.log(2)\n",
        "print(xtrue)\n",
        "print(f(xtrue))"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Now let's run Newton's method and keep track of the errors:"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 5,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "errors = []\n",
        "x = 2\n",
        "xbefore = 3"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "At each iteration, print the current guess and the error."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 17,
      "metadata": {
        "collapsed": false
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "nan\n",
            "nan\n"
          ]
        },
        {
          "name": "stderr",
          "output_type": "stream",
          "text": [
            "/home/andreas/src/env-3.7/lib/python3.7/site-packages/ipykernel_launcher.py:2: RuntimeWarning: invalid value encountered in double_scalars\n",
            "  \n"
          ]
        }
      ],
      "source": [
        "slope = (f(x)-f(xbefore))/(x-xbefore)\n",
        "\n",
        "xbefore = x\n",
        "x = x - f(x)/slope\n",
        "print(x)\n",
        "errors.append(abs(x-xtrue))\n",
        "print(errors[-1])"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 18,
      "metadata": {
        "collapsed": false
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "0.8824000774932711\n",
            "0.411823511031029\n",
            "0.14748204487642924\n",
            "0.027685962340313952\n",
            "0.0019826842906380815\n",
            "2.731067240058227e-05\n",
            "2.706515089823114e-08\n",
            "3.695932448977146e-13\n",
            "1.1102230246251565e-16\n",
            "1.1102230246251565e-16\n",
            "nan\n"
          ]
        }
      ],
      "source": [
        "for err in errors:\n",
        "    print(err)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "* Do you have a hypothesis about the order of convergence?"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 19,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "# Does not quite double the number of digits each round--unclear."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "------------\n",
        "Let's check:"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 19,
      "metadata": {
        "collapsed": false
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "0.5042249099646489\n",
            "0.6196358421422753\n",
            "0.6126884285565872\n",
            "0.6571696439293109\n",
            "0.6447273943575542\n",
            "0.6552765724242319\n",
            "0.6487597717813403\n",
            "14482.140529886734\n",
            "7243300082.988035\n",
            "nan\n"
          ]
        }
      ],
      "source": [
        "for i in range(len(errors)-1):\n",
        "    print(errors[i+1]/errors[i]**1.618)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": []
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.7.2+"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 2
}